import axios from 'axios'
// import { Store } from 'vuex';


const baseURL = 'http://localhost:8888/api/private/v1/'

//aious设置基础URL  全局的axios默认值
axios.defaults.baseURL = baseURL

// axios请求拦截器
axios.interceptors.request.use(function(config) {
    // 在发送请求之前做些什么
    let token = localStorage.getItem('mytoken');
    if (token) {
        config.headers['Authorization'] = token //把token设置成Authorization
    }
    return config; //请求参数
}, function(error) {
    // 对请求错误做些什么
    console.log('请求失败~')
    return Promise.reject(error);
});




//登录验证
export const checkUser = params => {
    return axios.post('login', params).then(res => res.data)
}

//获取用户列表
export const getUserList = params => {
    return axios.get('users', params).then(res => res.data)
}

//修改用户状态
export const changeUserState = params => {
    return axios.put(`users/${params.uid}/state/${params.type}`).then(res => res.data)
}

//添加用户
export const addNewUser = params => {
    return axios.post('users', params).then(res => res.data)
}

//根据id查询用户信息
export const editUserMessage = params => {
    return axios.get(`users/${params.id}`).then(res => res.data)
}

//编辑用户提交
export const editUser = params => {
    return axios.put(`users/${params.id}`, params).then(res => res.data)
}

//删除单个用户
export const deleteUserMessage = params => {
    return axios.delete(`users/${params}`, params).then(res => res.data)
}

//角色列表获取
export const getRoleList = parmas => {
    return axios.get('roles').then(res => res.data)
}

//分配角色
export const grantUserRole = params => {
    return axios.put(`users/${params.id}/role`, { id: params.id, rid: params.rid }).then(res => res.data)
}

//获取所有权限列表
export const getRightsList = params => {
    return axios.get(`rights/${params.type}`).then(res => res.data)
}

//删除角色指定权限
export const deleteRolesRights = params => {
    return axios.delete(`roles/${params.roleId}/rights/${params.rightId}`).then(res => res.data)
}

//角色授权
export const grantRolesRights = params => {
    return axios.post(`roles/${params.roleId}/rights`, params).then(res => res.data)
}